var express = require('express');
var router = express.Router();
const Cart = require("../beans/carts")
const cartController = require("../controllers/cart_controller")
const mongoUtil = require("../models/mongo_util")
const cartModel = require("../models/cart_model")
router.get('/', function (req, res, next) {
    res.send('respond with a resource');
});
// 添加购物车
// var goods = {
//     namr: 123,
//     id: "12121211323",
//     price: 11111,
//     qty: 1,
//     goodsId: 1003
// }
// 添加购物车
router.post("/cart", (req, res) => {
    // const Authorization=req.get

    const {
        userid,
        // token,
        // goods为对象
        goods
    } = req.body;
    const useridgoods = {
        'userid': userid
    }
    if (Object.prototype.toString.call(goods) == '[object Object]') {
        cartController.AddCartlist(useridgoods, (result) => {

            console.log("////", result.data.length);
            if (result.code == 200 && result.data.length > 0) {
                var whereStr = {
                    'userid': useridgoods.userid,
                    // 'goodsId':goodsId
                }
                result.data.forEach((item) => {
                    item.goodslist.unshift(goods)
                    console.log("item.goodslist", item.goodslist);
                    // var cartgoods = new Cart(
                    //     goodslist = item.goodslist
                    // )
                    var updateStr = {
                        $set: {
                            'goodslist': item.goodslist
                        }
                    };
                    mongoUtil.getCollection("cart").then(collection => {

                        collection.updateOne(whereStr, updateStr, function (err, res) {
                            console.log("插入成功");
                        });
                    })
                })
            } else {
                const cartgoods = new Cart(
                    userid,
                    goodslist = [
                        goods
                    ]
                )
                mongoUtil.getCollection('cart').then(collection => {
                    collection.insertOne(cartgoods, function (err, res) {
                        // callback(err, res)
                    });
                })
            }

            res.json(result)
        })
    } else {
        res.json({
            msg: "goods不是对象,添加购物车失败",
            code: 404
        })
    }

})
// 获取购物车商品
router.post('/getcartlist', (req, res) => {
    const {
        userid
    } = req.body; //获取id
    //通过id查询并将获取到的data返回
    const cart = {
        userid,
    }


    cartController.getCartlist(cart, (result) => {
        res.json(result)
    })
});
// 修改购物车商品数量
router.post('/updatacartgoodsqty', (req, res) => {
    // const Authorization=req.query.headers
    // console.log(Authorization);
    var {
        userid,
        qty,
        goodsId
    } = req.body
    const count = {
        userid,
        // qty,
        // goodsId
    }
    cartController.updatacartgoodsqty(count, (result) => {
        if (result.code == 200) {
            result.data.forEach((item) => {

                item.goodslist.forEach((it) => {
                    console.log("Object.prototype.toString.call(goods)", Object.prototype.toString.call(goodsId));
                    if (it.goodsId == goodsId) {
                        // class WhereStr {
                        //     constructor(id) {
                        //         this.userid = id
                        //     }
                        // }
                        var num = Number(it.qty) + qty * 1
                        // const whereStr = new WhereStr(userid)
                        var whereStr = {
                            'userid': userid,
                            // 'goodsId':goodsId
                        }
                        console.log("1212121212", whereStr);
                        it['qty'] = num
                        console.log("00000", it);
                        var updateStr = {
                            $set: {
                                'goodslist': item.goodslist
                            }
                        };
                        mongoUtil.getCollection("cart").then(collection => {

                            collection.updateOne(whereStr, updateStr, function (err, res) {
                                console.log("修改成功");
                            });
                        })
                    }
                })

            })
        }
        res.json(result)
    })
})
//删除购物车商品
router.delete("/deletecartgoods", (req, res) => {
    var {
        userid,
        goodsId
    } = req.body
    // 传过来的goodsId为一个数组
    var goodsId = [1003]
    const delGoods = {
        userid,
        // goodsId
    }
    cartController.deletecartgoods(delGoods, (result) => {
        if (result.code == 200) {
            result.data.forEach(item => {
                var goodslist = item.goodslist.filter(pre => {
                    console.log("goodsID", goodsId * 1, pre);
                    console.log("pre=", goodsId.includes(pre.goodsId));
                    return !goodsId.includes(pre.goodsId)
                })
                mongoUtil.getCollection("cart").then(collection => {
                    collection.updateOne({
                        'userid': userid
                    }, {
                        $set: {
                            'goodslist': goodslist
                        }
                    }, function (err, resp) {
                        res.json({
                            Code: 200,
                            message: "删除商品成功",
                            data: null
                        })
                    });
                })
            })
        } else {
            res.json({
                msg: "删除商品失败",
                code: 401
            })
        }

    })
})



module.exports = router;